@@ -305,10 +305,15 @@ static bool isNoopPtrIntCastPair(const Operator *I2P, const DataLayout &DL,
305
305
}
306
306
307
307
// Returns true if V is an address expression.
308
- // TODO: Currently, we consider only phi, bitcast, addrspacecast, and
309
- // getelementptr operators.
308
+ // TODO: Currently, we consider only arguments and phi, bitcast, addrspacecast,
309
+ // and getelementptr operators.
310
310
static bool isAddressExpression (const Value &V, const DataLayout &DL,
311
311
const TargetTransformInfo *TTI) {
312
+
313
+ if (const Argument *Arg = dyn_cast<Argument>(&V))
314
+ return Arg->getType ()->isPointerTy () &&
315
+ TTI->getAssumedAddrSpace (&V) != UninitializedAddressSpace;
316
+
312
317
const Operator *Op = dyn_cast<Operator>(&V);
313
318
if (!Op)
314
319
return false ;
@@ -341,6 +346,9 @@ static bool isAddressExpression(const Value &V, const DataLayout &DL,
341
346
static SmallVector<Value *, 2 >
342
347
getPointerOperands (const Value &V, const DataLayout &DL,
343
348
const TargetTransformInfo *TTI) {
349
+ if (isa<Argument>(&V))
350
+ return {};
351
+
344
352
const Operator &Op = cast<Operator>(V);
345
353
switch (Op.getOpcode ()) {
346
354
case Instruction::PHI: {
@@ -505,13 +513,11 @@ void InferAddressSpacesImpl::appendsFlatAddressExpressionToPostorderStack(
505
513
if (Visited.insert (V).second ) {
506
514
PostorderStack.emplace_back (V, false );
507
515
508
- Operator *Op = cast<Operator>(V);
509
- for (unsigned I = 0 , E = Op->getNumOperands (); I != E; ++I) {
510
- if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op->getOperand (I))) {
511
- if (isAddressExpression (*CE, *DL, TTI) && Visited.insert (CE).second )
512
- PostorderStack.emplace_back (CE, false );
513
- }
514
- }
516
+ if (auto *Op = dyn_cast<Operator>(V))
517
+ for (auto &O : Op->operands ())
518
+ if (ConstantExpr *CE = dyn_cast<ConstantExpr>(O))
519
+ if (isAddressExpression (*CE, *DL, TTI) && Visited.insert (CE).second )
520
+ PostorderStack.emplace_back (CE, false );
515
521
}
516
522
}
517
523
}
@@ -828,6 +834,18 @@ Value *InferAddressSpacesImpl::cloneValueWithNewAddressSpace(
828
834
assert (V->getType ()->getPointerAddressSpace () == FlatAddrSpace &&
829
835
isAddressExpression (*V, *DL, TTI));
830
836
837
+ if (auto *Arg = dyn_cast<Argument>(V)) {
838
+ // Arguments are address space casted in the function body, as we do not
839
+ // want to change the function signature.
840
+ Function *F = Arg->getParent ();
841
+ BasicBlock::iterator Insert = F->getEntryBlock ().getFirstNonPHIIt ();
842
+
843
+ Type *NewPtrTy = PointerType::get (Arg->getContext (), NewAddrSpace);
844
+ auto *NewI = new AddrSpaceCastInst (Arg, NewPtrTy);
845
+ NewI->insertBefore (Insert);
846
+ return NewI;
847
+ }
848
+
831
849
if (Instruction *I = dyn_cast<Instruction>(V)) {
832
850
Value *NewV = cloneInstructionWithNewAddressSpace (
833
851
I, NewAddrSpace, ValueWithNewAddrSpace, PredicatedAS, PoisonUsesToFix);
@@ -966,8 +984,9 @@ bool InferAddressSpacesImpl::updateAddressSpace(
966
984
// of all its pointer operands.
967
985
unsigned NewAS = UninitializedAddressSpace;
968
986
969
- const Operator &Op = cast<Operator>(V);
970
- if (Op.getOpcode () == Instruction::Select) {
987
+ if (isa<Operator>(V) &&
988
+ cast<Operator>(V).getOpcode () == Instruction::Select) {
989
+ const Operator &Op = cast<Operator>(V);
971
990
Value *Src0 = Op.getOperand (1 );
972
991
Value *Src1 = Op.getOperand (2 );
973
992
@@ -1275,7 +1294,7 @@ void InferAddressSpacesImpl::performPointerReplacement(
1275
1294
// This instruction may contain multiple uses of V, update them all.
1276
1295
CurUser->replaceUsesOfWith (
1277
1296
V, new AddrSpaceCastInst (NewV, V->getType (), " " , InsertPos));
1278
- } else {
1297
+ } else if (isa<Constant>(V)) {
1279
1298
CurUserI->replaceUsesOfWith (
1280
1299
V, ConstantExpr::getAddrSpaceCast (cast<Constant>(NewV), V->getType ()));
1281
1300
}
0 commit comments