@@ -91,8 +91,47 @@ bool InstCombinerImpl::SimplifyDemandedBits(Instruction *I, unsigned OpNo,
91
91
KnownBits &Known, unsigned Depth,
92
92
const SimplifyQuery &Q) {
93
93
Use &U = I->getOperandUse (OpNo);
94
- Value *NewVal = SimplifyDemandedUseBits (U.get (), DemandedMask, Known,
95
- Depth, Q);
94
+ Value *V = U.get ();
95
+ if (isa<Constant>(V)) {
96
+ llvm::computeKnownBits (V, Known, Depth, Q);
97
+ return false ;
98
+ }
99
+
100
+ Known.resetAll ();
101
+ if (DemandedMask.isZero ()) {
102
+ // Not demanding any bits from V.
103
+ replaceUse (U, UndefValue::get (V->getType ()));
104
+ return true ;
105
+ }
106
+
107
+ if (Depth == MaxAnalysisRecursionDepth)
108
+ return false ;
109
+
110
+ Instruction *VInst = dyn_cast<Instruction>(V);
111
+ if (!VInst) {
112
+ llvm::computeKnownBits (V, Known, Depth, Q);
113
+ return false ;
114
+ }
115
+
116
+ Value *NewVal;
117
+ if (VInst->hasOneUse ()) {
118
+ // If the instruction has one use, we can directly simplify it.
119
+ NewVal = SimplifyDemandedUseBits (VInst, DemandedMask, Known, Depth, Q);
120
+ } else if (Depth != 0 ) {
121
+ // If there are multiple uses of this instruction and we aren't at the root,
122
+ // then we can simplify VInst to some other value, but not modify the
123
+ // instruction.
124
+ NewVal =
125
+ SimplifyMultipleUseDemandedBits (VInst, DemandedMask, Known, Depth, Q);
126
+ } else {
127
+ // If this is the root being simplified, allow it to have multiple uses,
128
+ // just set the DemandedMask to all bits and reset the context instruction.
129
+ // This allows visitTruncInst (for example) to simplify the operand of a
130
+ // trunc without duplicating all the SimplifyDemandedUseBits() logic.
131
+ NewVal =
132
+ SimplifyDemandedUseBits (VInst, APInt::getAllOnes (Known.getBitWidth ()),
133
+ Known, Depth, Q.getWithInstruction (VInst));
134
+ }
96
135
if (!NewVal) return false ;
97
136
if (Instruction* OpInst = dyn_cast<Instruction>(U))
98
137
salvageDebugInfo (*OpInst);
@@ -124,50 +163,21 @@ bool InstCombinerImpl::SimplifyDemandedBits(Instruction *I, unsigned OpNo,
124
163
// / operands based on the information about what bits are demanded. This returns
125
164
// / some other non-null value if it found out that V is equal to another value
126
165
// / in the context where the specified bits are demanded, but not for all users.
127
- Value *InstCombinerImpl::SimplifyDemandedUseBits (Value *V, APInt DemandedMask,
166
+ Value *InstCombinerImpl::SimplifyDemandedUseBits (Instruction *I,
167
+ const APInt &DemandedMask,
128
168
KnownBits &Known,
129
169
unsigned Depth,
130
170
const SimplifyQuery &Q) {
131
- assert (V != nullptr && " Null pointer of Value???" );
171
+ assert (I != nullptr && " Null pointer of Value???" );
132
172
assert (Depth <= MaxAnalysisRecursionDepth && " Limit Search Depth" );
133
173
uint32_t BitWidth = DemandedMask.getBitWidth ();
134
- Type *VTy = V ->getType ();
174
+ Type *VTy = I ->getType ();
135
175
assert (
136
176
(!VTy->isIntOrIntVectorTy () || VTy->getScalarSizeInBits () == BitWidth) &&
137
177
Known.getBitWidth () == BitWidth &&
138
178
" Value *V, DemandedMask and Known must have same BitWidth" );
139
179
140
- if (isa<Constant>(V)) {
141
- llvm::computeKnownBits (V, Known, Depth, Q);
142
- return nullptr ;
143
- }
144
-
145
- Known.resetAll ();
146
- if (DemandedMask.isZero ()) // Not demanding any bits from V.
147
- return UndefValue::get (VTy);
148
-
149
- if (Depth == MaxAnalysisRecursionDepth)
150
- return nullptr ;
151
-
152
- Instruction *I = dyn_cast<Instruction>(V);
153
- if (!I) {
154
- llvm::computeKnownBits (V, Known, Depth, Q);
155
- return nullptr ; // Only analyze instructions.
156
- }
157
-
158
- // If there are multiple uses of this value and we aren't at the root, then
159
- // we can't do any simplifications of the operands, because DemandedMask
160
- // only reflects the bits demanded by *one* of the users.
161
- if (Depth != 0 && !I->hasOneUse ())
162
- return SimplifyMultipleUseDemandedBits (I, DemandedMask, Known, Depth, Q);
163
-
164
180
KnownBits LHSKnown (BitWidth), RHSKnown (BitWidth);
165
- // If this is the root being simplified, allow it to have multiple uses,
166
- // just set the DemandedMask to all bits so that we can try to simplify the
167
- // operands. This allows visitTruncInst (for example) to simplify the
168
- // operand of a trunc without duplicating all the logic below.
169
- if (Depth == 0 && !V->hasOneUse ())
170
- DemandedMask.setAllBits ();
171
181
172
182
// Update flags after simplifying an operand based on the fact that some high
173
183
// order bits are not demanded.
@@ -1105,27 +1115,28 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
1105
1115
}
1106
1116
1107
1117
if (!KnownBitsComputed)
1108
- llvm::computeKnownBits (V , Known, Depth, Q);
1118
+ llvm::computeKnownBits (I , Known, Depth, Q);
1109
1119
break ;
1110
1120
}
1111
1121
}
1112
1122
1113
- if (V ->getType ()->isPointerTy ()) {
1114
- Align Alignment = V ->getPointerAlignment (DL);
1123
+ if (I ->getType ()->isPointerTy ()) {
1124
+ Align Alignment = I ->getPointerAlignment (DL);
1115
1125
Known.Zero .setLowBits (Log2 (Alignment));
1116
1126
}
1117
1127
1118
1128
// If the client is only demanding bits that we know, return the known
1119
1129
// constant. We can't directly simplify pointers as a constant because of
1120
1130
// pointer provenance.
1121
1131
// TODO: We could return `(inttoptr const)` for pointers.
1122
- if (!V->getType ()->isPointerTy () && DemandedMask.isSubsetOf (Known.Zero | Known.One ))
1132
+ if (!I->getType ()->isPointerTy () &&
1133
+ DemandedMask.isSubsetOf (Known.Zero | Known.One ))
1123
1134
return Constant::getIntegerValue (VTy, Known.One );
1124
1135
1125
1136
if (VerifyKnownBits) {
1126
- KnownBits ReferenceKnown = llvm::computeKnownBits (V , Depth, Q);
1137
+ KnownBits ReferenceKnown = llvm::computeKnownBits (I , Depth, Q);
1127
1138
if (Known != ReferenceKnown) {
1128
- errs () << " Mismatched known bits for " << *V << " in "
1139
+ errs () << " Mismatched known bits for " << *I << " in "
1129
1140
<< I->getFunction ()->getName () << " \n " ;
1130
1141
errs () << " computeKnownBits(): " << ReferenceKnown << " \n " ;
1131
1142
errs () << " SimplifyDemandedBits(): " << Known << " \n " ;
0 commit comments