Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 63e73b3

Browse files
committed
LLVM's stripPointerCasts can now look through a call/invoke (possibly with side effects) if it has a param with the 'returned' attribute
1 parent 39715d8 commit 63e73b3

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

lib/Target/JSBackend/CallHandlers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const Value *getActuallyCalledValue(const Instruction *I) {
2424
// for example, extern void x() in C will turn into void x(...) in LLVM IR, then the IR bitcasts
2525
// it to the proper form right before the call. this both causes an unnecessary indirect
2626
// call, and it is done with the wrong type. TODO: don't even put it into the function table
27-
if (const Function *F = dyn_cast<const Function>(CV->stripPointerCasts())) {
27+
if (const Function *F = dyn_cast<const Function>(stripPointerCastsWithoutSideEffects(CV))) {
2828
CV = F;
2929
}
3030
return CV;

lib/Target/JSBackend/JSBackend.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,19 @@ namespace {
299299
raw_pwrite_stream& nl(raw_pwrite_stream &Out, int delta = 0);
300300

301301
private:
302+
303+
// LLVM changed stripPointerCasts to use the "returned" attribute on
304+
// calls and invokes, i.e., stripping pointer casts of a call to
305+
// define internal i8* @strupr(i8* returned %str) #2 {
306+
// will return the pointer, and ignore the call which has side
307+
// effects. We sometimes do care about the side effects.
308+
const Value* stripPointerCastsWithoutSideEffects(const Value* V) {
309+
if (isa<CallInst>(V) || isa<InvokeInst>(V)) {
310+
return V; // in theory we could check if there actually are side effects
311+
}
312+
return V->stripPointerCasts();
313+
}
314+
302315
void printCommaSeparated(const HeapData v);
303316

304317
// parsing of constants has two phases: calculate, and then emit
@@ -1679,7 +1692,7 @@ std::string JSWriter::getConstantVector(const ConstantVectorType *C) {
16791692

16801693
std::string JSWriter::getValueAsStr(const Value* V, AsmCast sign) {
16811694
// Skip past no-op bitcasts and zero-index geps.
1682-
V = V->stripPointerCasts();
1695+
V = stripPointerCastsWithoutSideEffects(V);
16831696

16841697
if (const Constant *CV = dyn_cast<Constant>(V)) {
16851698
return getConstant(CV, sign);
@@ -1690,7 +1703,7 @@ std::string JSWriter::getValueAsStr(const Value* V, AsmCast sign) {
16901703

16911704
std::string JSWriter::getValueAsCastStr(const Value* V, AsmCast sign) {
16921705
// Skip past no-op bitcasts and zero-index geps.
1693-
V = V->stripPointerCasts();
1706+
V = stripPointerCastsWithoutSideEffects(V);
16941707

16951708
if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) {
16961709
return getConstant(cast<Constant>(V), sign);
@@ -1701,7 +1714,7 @@ std::string JSWriter::getValueAsCastStr(const Value* V, AsmCast sign) {
17011714

17021715
std::string JSWriter::getValueAsParenStr(const Value* V) {
17031716
// Skip past no-op bitcasts and zero-index geps.
1704-
V = V->stripPointerCasts();
1717+
V = stripPointerCastsWithoutSideEffects(V);
17051718

17061719
if (const Constant *CV = dyn_cast<Constant>(V)) {
17071720
return getConstant(CV);
@@ -1712,7 +1725,7 @@ std::string JSWriter::getValueAsParenStr(const Value* V) {
17121725

17131726
std::string JSWriter::getValueAsCastParenStr(const Value* V, AsmCast sign) {
17141727
// Skip past no-op bitcasts and zero-index geps.
1715-
V = V->stripPointerCasts();
1728+
V = stripPointerCastsWithoutSideEffects(V);
17161729

17171730
if (isa<ConstantInt>(V) || isa<ConstantFP>(V) || isa<UndefValue>(V)) {
17181731
return getConstant(cast<Constant>(V), sign);
@@ -2362,7 +2375,7 @@ void JSWriter::generateExpression(const User *I, raw_string_ostream& Code) {
23622375
// and all-zero-index geps that LLVM needs to satisfy its type system, we
23632376
// call stripPointerCasts() on all values before translating them. This
23642377
// includes bitcasts whose only use is lifetime marker intrinsics.
2365-
assert(I == I->stripPointerCasts());
2378+
assert(I == stripPointerCastsWithoutSideEffects(I));
23662379

23672380
Type *T = I->getType();
23682381
if (T->isIntegerTy() && ((!OnlyWebAssembly && T->getIntegerBitWidth() > 32) ||
@@ -2961,7 +2974,7 @@ void JSWriter::addBlock(const BasicBlock *BB, Relooper& R, LLVMToRelooperMap& LL
29612974
for (BasicBlock::const_iterator II = BB->begin(), E = BB->end();
29622975
II != E; ++II) {
29632976
auto I = &*II;
2964-
if (I->stripPointerCasts() == I) {
2977+
if (stripPointerCastsWithoutSideEffects(I) == I) {
29652978
CurrInstruction = I;
29662979
generateExpression(I, CodeStream);
29672980
}

0 commit comments

Comments
 (0)