LLVM: lib/Transforms/Scalar/BDCE.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

30

31using namespace llvm;

33

34#define DEBUG_TYPE "bdce"

35

36STATISTIC(NumRemoved, "Number of instructions removed (unused)");

37STATISTIC(NumSimplified, "Number of instructions trivialized (dead bits)");

39 "Number of sign extension instructions converted to zero extension");

40

41

42

43

45 assert(I->getType()->isIntOrIntVectorTy() &&

46 "Trivializing a non-integer value?");

47

48

49

50 if (DB.getDemandedBits(I).isAllOnes())

51 return;

52

53

56 for (User *JU : I->users()) {

58 if (J->getType()->isIntOrIntVectorTy()) {

61 }

62

63

64

65

66

67

68

69

70

71 }

72

73

74 while (!WorkList.empty()) {

76

77

79

80

81

82

83

84

85 if (DB.getDemandedBits(J).isAllOnes())

86 continue;

87

90 if (Visited.insert(K).second && K->getType()->isIntOrIntVectorTy())

92 }

93 }

94}

95

100

101

102

103 if (I.mayHaveSideEffects() && I.use_empty())

104 continue;

105

106

107

108 if (DB.isInstructionDead(&I) ||

109 (I.getType()->isIntOrIntVectorTy() && DB.getDemandedBits(&I).isZero() &&

113 continue;

114 }

115

116

118 APInt Demanded = DB.getDemandedBits(SE);

119 const uint32_t SrcBitSize = SE->getSrcTy()->getScalarSizeInBits();

120 auto *const DstTy = SE->getDestTy();

121 const uint32_t DestBitSize = DstTy->getScalarSizeInBits();

122 if (Demanded.countl_zero() >= (DestBitSize - SrcBitSize)) {

125 I.replaceAllUsesWith(

126 Builder.CreateZExt(SE->getOperand(0), DstTy, SE->getName()));

129 NumSExt2ZExt++;

130 continue;

131 }

132 }

133

134

136 APInt Demanded = DB.getDemandedBits(BO);

138 const APInt *Mask;

139 if (match(BO->getOperand(1), m_APInt(Mask))) {

140 bool CanBeSimplified = false;

141 switch (BO->getOpcode()) {

142 case Instruction::Or:

143 case Instruction::Xor:

144 CanBeSimplified = !Demanded.intersects(*Mask);

145 break;

146 case Instruction::And:

147 CanBeSimplified = Demanded.isSubsetOf(*Mask);

148 break;

149 default:

150

151 break;

152 }

153

154 if (CanBeSimplified) {

156 BO->replaceAllUsesWith(BO->getOperand(0));

158 ++NumSimplified;

160 continue;

161 }

162 }

163 }

164 }

165

166 for (Use &U : I.operands()) {

167

168 if (!U->getType()->isIntOrIntVectorTy())

169 continue;

170

172 continue;

173

174 if (!DB.isUseDead(&U))

175 continue;

176

177 LLVM_DEBUG(dbgs() << "BDCE: Trivializing: " << U << " (all bits dead)\n");

178

180

181

182

183 U.set(ConstantInt::get(U->getType(), 0));

184 ++NumSimplified;

186 }

187 }

188

191 I->dropAllReferences();

192 }

193

195 ++NumRemoved;

196 I->eraseFromParent();

197 }

198

200}

201

assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")

Expand Atomic instructions

static void clearAssumptionsOfUsers(Instruction *I, DemandedBits &DB)

If an instruction is trivialized (dead), then the chain of users of that instruction may need to be c...

Definition BDCE.cpp:44

static bool bitTrackingDCE(Function &F, DemandedBits &DB)

Definition BDCE.cpp:96

This is the interface for a simple mod/ref and alias analysis over globals.

This file defines the SmallPtrSet class.

This file defines the SmallVector class.

This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...

#define STATISTIC(VARNAME, DESC)

Class for arbitrary precision integers.

bool isAllOnes() const

Determine if all bits are set. This is true for zero-width values.

bool intersects(const APInt &RHS) const

This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...

unsigned countl_zero() const

The APInt version of std::countl_zero.

bool isSubsetOf(const APInt &RHS) const

This operation checks that all bits set in this APInt are also set in RHS.

PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)

Get the result of an analysis pass for a given IR unit.

Represents analyses that only rely on functions' control flow.

An analysis that produces DemandedBits for a function.

This provides a uniform API for creating instructions and inserting them into a basic block: either a...

void dropPoisonGeneratingAnnotations()

Drops flags, return attributes and metadata that may generate poison.

A set of analyses that are preserved following a run of a transformation pass.

static PreservedAnalyses all()

Construct a special preserved set that preserves all passes.

PreservedAnalyses & preserveSet()

Mark an analysis set as preserved.

This class represents a sign extension of integer types.

std::pair< iterator, bool > insert(PtrType Ptr)

Inserts Ptr if and only if there is no element in the container equal to Ptr.

SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.

void push_back(const T &Elt)

This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.

A Use represents the edge between a Value definition and its users.

iterator_range< user_iterator > users()

ap_match< APInt > m_APInt(const APInt *&Res)

Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.

bool match(Val *V, const Pattern &P)

This is an optimization pass for GlobalISel generic memory operations.

decltype(auto) dyn_cast(const From &Val)

dyn_cast - Return the argument parameter cast to the specified type.

LLVM_ABI void salvageDebugInfo(const MachineRegisterInfo &MRI, MachineInstr &MI)

Assuming the instruction MI is going to be deleted, attempt to salvage debug users of MI by writing t...

auto reverse(ContainerTy &&C)

LLVM_ABI raw_ostream & dbgs()

dbgs() - This returns a reference to a raw_ostream for debugging messages.

LLVM_ABI bool wouldInstructionBeTriviallyDead(const Instruction *I, const TargetLibraryInfo *TLI=nullptr)

Return true if the result produced by the instruction would have no side effects if it was not used.

bool isa(const From &Val)

isa - Return true if the parameter to the template is an instance of one of the template type argu...

decltype(auto) cast(const From &Val)

cast - Return the argument parameter cast to the specified type.

AnalysisManager< Function > FunctionAnalysisManager

Convenience typedef for the Function analysis manager.

PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)

Definition BDCE.cpp:202