2016-12-13 06:23:53 +08:00
|
|
|
//===- PPCBoolRetToInt.cpp ------------------------------------------------===//
|
2015-12-08 04:50:29 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-12-08 04:50:29 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2017-06-09 02:27:24 +08:00
|
|
|
// This file implements converting i1 values to i32/i64 if they could be more
|
2015-12-08 04:50:29 +08:00
|
|
|
// profitably allocated as GPRs rather than CRs. This pass will become totally
|
|
|
|
// unnecessary if Register Bank Allocation and Global Instruction Selection ever
|
|
|
|
// go upstream.
|
|
|
|
//
|
2017-06-09 02:27:24 +08:00
|
|
|
// Presently, the pass converts i1 Constants, and Arguments to i32/i64 if the
|
2015-12-08 04:50:29 +08:00
|
|
|
// transitive closure of their uses includes only PHINodes, CallInsts, and
|
|
|
|
// ReturnInsts. The rational is that arguments are generally passed and returned
|
2017-06-09 02:27:24 +08:00
|
|
|
// in GPRs rather than CRs, so casting them to i32/i64 at the LLVM IR level will
|
2015-12-08 04:50:29 +08:00
|
|
|
// actually save casts at the Machine Instruction level.
|
|
|
|
//
|
|
|
|
// It might be useful to expand this pass to add bit-wise operations to the list
|
|
|
|
// of safe transitive closure types. Also, we miss some opportunities when LLVM
|
|
|
|
// represents logical AND and OR operations with control flow rather than data
|
|
|
|
// flow. For example by lowering the expression: return (A && B && C)
|
|
|
|
//
|
|
|
|
// as: return A ? true : B && C.
|
|
|
|
//
|
|
|
|
// There's code in SimplifyCFG that code be used to turn control flow in data
|
|
|
|
// flow using SelectInsts. Selects are slow on some architectures (P7/P8), so
|
|
|
|
// this probably isn't good in general, but for the special case of i1, the
|
|
|
|
// Selects could be further lowered to bit operations that are fast everywhere.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "PPC.h"
|
2017-06-09 02:27:24 +08:00
|
|
|
#include "PPCTargetMachine.h"
|
2016-12-13 06:23:53 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2015-12-08 04:50:29 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2016-12-13 06:23:53 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2015-12-08 04:50:29 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2016-12-13 06:23:53 +08:00
|
|
|
#include "llvm/IR/Argument.h"
|
2015-12-08 04:50:29 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/Dominators.h"
|
2016-12-13 06:23:53 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instruction.h"
|
2015-12-08 04:50:29 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2016-12-13 06:23:53 +08:00
|
|
|
#include "llvm/IR/OperandTraits.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
|
|
|
#include "llvm/IR/Use.h"
|
|
|
|
#include "llvm/IR/User.h"
|
|
|
|
#include "llvm/IR/Value.h"
|
2015-12-08 04:50:29 +08:00
|
|
|
#include "llvm/Pass.h"
|
2017-06-09 02:27:24 +08:00
|
|
|
#include "llvm/CodeGen/TargetPassConfig.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2016-12-13 06:23:53 +08:00
|
|
|
#include <cassert>
|
2015-12-08 04:50:29 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "bool-ret-to-int"
|
|
|
|
|
|
|
|
STATISTIC(NumBoolRetPromotion,
|
|
|
|
"Number of times a bool feeding a RetInst was promoted to an int");
|
|
|
|
STATISTIC(NumBoolCallPromotion,
|
|
|
|
"Number of times a bool feeding a CallInst was promoted to an int");
|
|
|
|
STATISTIC(NumBoolToIntPromotion,
|
|
|
|
"Total number of times a bool was promoted to an int");
|
|
|
|
|
|
|
|
class PPCBoolRetToInt : public FunctionPass {
|
|
|
|
static SmallPtrSet<Value *, 8> findAllDefs(Value *V) {
|
|
|
|
SmallPtrSet<Value *, 8> Defs;
|
|
|
|
SmallVector<Value *, 8> WorkList;
|
|
|
|
WorkList.push_back(V);
|
|
|
|
Defs.insert(V);
|
|
|
|
while (!WorkList.empty()) {
|
|
|
|
Value *Curr = WorkList.back();
|
|
|
|
WorkList.pop_back();
|
2016-12-13 06:23:53 +08:00
|
|
|
auto *CurrUser = dyn_cast<User>(Curr);
|
2016-08-04 05:43:51 +08:00
|
|
|
// Operands of CallInst are skipped because they may not be Bool type,
|
|
|
|
// and their positions are defined by ABI.
|
|
|
|
if (CurrUser && !isa<CallInst>(Curr))
|
2015-12-08 04:50:29 +08:00
|
|
|
for (auto &Op : CurrUser->operands())
|
|
|
|
if (Defs.insert(Op).second)
|
|
|
|
WorkList.push_back(Op);
|
|
|
|
}
|
|
|
|
return Defs;
|
|
|
|
}
|
|
|
|
|
2017-06-09 02:27:24 +08:00
|
|
|
// Translate a i1 value to an equivalent i32/i64 value:
|
|
|
|
Value *translate(Value *V) {
|
|
|
|
Type *IntTy = ST->isPPC64() ? Type::getInt64Ty(V->getContext())
|
|
|
|
: Type::getInt32Ty(V->getContext());
|
|
|
|
|
2016-12-13 06:23:53 +08:00
|
|
|
if (auto *C = dyn_cast<Constant>(V))
|
2017-06-09 02:27:24 +08:00
|
|
|
return ConstantExpr::getZExt(C, IntTy);
|
2016-12-13 06:23:53 +08:00
|
|
|
if (auto *P = dyn_cast<PHINode>(V)) {
|
2015-12-08 04:50:29 +08:00
|
|
|
// Temporarily set the operands to 0. We'll fix this later in
|
|
|
|
// runOnUse.
|
2017-06-09 02:27:24 +08:00
|
|
|
Value *Zero = Constant::getNullValue(IntTy);
|
2015-12-08 04:50:29 +08:00
|
|
|
PHINode *Q =
|
2017-06-09 02:27:24 +08:00
|
|
|
PHINode::Create(IntTy, P->getNumIncomingValues(), P->getName(), P);
|
2015-12-08 04:50:29 +08:00
|
|
|
for (unsigned i = 0; i < P->getNumOperands(); ++i)
|
|
|
|
Q->addIncoming(Zero, P->getIncomingBlock(i));
|
|
|
|
return Q;
|
|
|
|
}
|
|
|
|
|
2016-12-13 06:23:53 +08:00
|
|
|
auto *A = dyn_cast<Argument>(V);
|
|
|
|
auto *I = dyn_cast<Instruction>(V);
|
2015-12-08 04:50:29 +08:00
|
|
|
assert((A || I) && "Unknown value type");
|
|
|
|
|
|
|
|
auto InstPt =
|
|
|
|
A ? &*A->getParent()->getEntryBlock().begin() : I->getNextNode();
|
2017-06-09 02:27:24 +08:00
|
|
|
return new ZExtInst(V, IntTy, "", InstPt);
|
2015-12-08 04:50:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef SmallPtrSet<const PHINode *, 8> PHINodeSet;
|
|
|
|
|
|
|
|
// A PHINode is Promotable if:
|
|
|
|
// 1. Its type is i1 AND
|
|
|
|
// 2. All of its uses are ReturnInt, CallInst, PHINode, or DbgInfoIntrinsic
|
|
|
|
// AND
|
|
|
|
// 3. All of its operands are Constant or Argument or
|
|
|
|
// CallInst or PHINode AND
|
|
|
|
// 4. All of its PHINode uses are Promotable AND
|
|
|
|
// 5. All of its PHINode operands are Promotable
|
|
|
|
static PHINodeSet getPromotablePHINodes(const Function &F) {
|
|
|
|
PHINodeSet Promotable;
|
|
|
|
// Condition 1
|
|
|
|
for (auto &BB : F)
|
|
|
|
for (auto &I : BB)
|
2016-12-13 06:23:53 +08:00
|
|
|
if (const auto *P = dyn_cast<PHINode>(&I))
|
2015-12-08 04:50:29 +08:00
|
|
|
if (P->getType()->isIntegerTy(1))
|
|
|
|
Promotable.insert(P);
|
|
|
|
|
|
|
|
SmallVector<const PHINode *, 8> ToRemove;
|
2016-02-22 21:11:58 +08:00
|
|
|
for (const PHINode *P : Promotable) {
|
2015-12-08 04:50:29 +08:00
|
|
|
// Condition 2 and 3
|
|
|
|
auto IsValidUser = [] (const Value *V) -> bool {
|
|
|
|
return isa<ReturnInst>(V) || isa<CallInst>(V) || isa<PHINode>(V) ||
|
|
|
|
isa<DbgInfoIntrinsic>(V);
|
|
|
|
};
|
|
|
|
auto IsValidOperand = [] (const Value *V) -> bool {
|
|
|
|
return isa<Constant>(V) || isa<Argument>(V) || isa<CallInst>(V) ||
|
|
|
|
isa<PHINode>(V);
|
|
|
|
};
|
|
|
|
const auto &Users = P->users();
|
|
|
|
const auto &Operands = P->operands();
|
2016-12-13 06:23:53 +08:00
|
|
|
if (!llvm::all_of(Users, IsValidUser) ||
|
|
|
|
!llvm::all_of(Operands, IsValidOperand))
|
2015-12-08 04:50:29 +08:00
|
|
|
ToRemove.push_back(P);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate to convergence
|
|
|
|
auto IsPromotable = [&Promotable] (const Value *V) -> bool {
|
2016-12-13 06:23:53 +08:00
|
|
|
const auto *Phi = dyn_cast<PHINode>(V);
|
2015-12-08 04:50:29 +08:00
|
|
|
return !Phi || Promotable.count(Phi);
|
|
|
|
};
|
|
|
|
while (!ToRemove.empty()) {
|
|
|
|
for (auto &User : ToRemove)
|
|
|
|
Promotable.erase(User);
|
|
|
|
ToRemove.clear();
|
|
|
|
|
2016-02-22 21:11:58 +08:00
|
|
|
for (const PHINode *P : Promotable) {
|
2015-12-08 04:50:29 +08:00
|
|
|
// Condition 4 and 5
|
|
|
|
const auto &Users = P->users();
|
|
|
|
const auto &Operands = P->operands();
|
2016-12-13 06:23:53 +08:00
|
|
|
if (!llvm::all_of(Users, IsPromotable) ||
|
|
|
|
!llvm::all_of(Operands, IsPromotable))
|
2015-12-08 04:50:29 +08:00
|
|
|
ToRemove.push_back(P);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promotable;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef DenseMap<Value *, Value *> B2IMap;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
2016-12-13 06:23:53 +08:00
|
|
|
|
2017-03-31 10:16:54 +08:00
|
|
|
PPCBoolRetToInt() : FunctionPass(ID) {
|
2015-12-08 04:50:29 +08:00
|
|
|
initializePPCBoolRetToIntPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
2016-12-13 06:23:53 +08:00
|
|
|
bool runOnFunction(Function &F) override {
|
2016-04-28 03:39:32 +08:00
|
|
|
if (skipFunction(F))
|
|
|
|
return false;
|
|
|
|
|
2017-06-09 02:27:24 +08:00
|
|
|
auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
|
|
|
|
if (!TPC)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto &TM = TPC->getTM<PPCTargetMachine>();
|
|
|
|
ST = TM.getSubtargetImpl(F);
|
|
|
|
|
2015-12-08 04:50:29 +08:00
|
|
|
PHINodeSet PromotablePHINodes = getPromotablePHINodes(F);
|
|
|
|
B2IMap Bool2IntMap;
|
|
|
|
bool Changed = false;
|
|
|
|
for (auto &BB : F) {
|
|
|
|
for (auto &I : BB) {
|
2016-12-13 06:23:53 +08:00
|
|
|
if (auto *R = dyn_cast<ReturnInst>(&I))
|
2015-12-08 04:50:29 +08:00
|
|
|
if (F.getReturnType()->isIntegerTy(1))
|
|
|
|
Changed |=
|
|
|
|
runOnUse(R->getOperandUse(0), PromotablePHINodes, Bool2IntMap);
|
|
|
|
|
2016-12-13 06:23:53 +08:00
|
|
|
if (auto *CI = dyn_cast<CallInst>(&I))
|
2015-12-08 04:50:29 +08:00
|
|
|
for (auto &U : CI->operands())
|
|
|
|
if (U->getType()->isIntegerTy(1))
|
|
|
|
Changed |= runOnUse(U, PromotablePHINodes, Bool2IntMap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2017-06-09 02:27:24 +08:00
|
|
|
bool runOnUse(Use &U, const PHINodeSet &PromotablePHINodes,
|
2015-12-08 04:50:29 +08:00
|
|
|
B2IMap &BoolToIntMap) {
|
|
|
|
auto Defs = findAllDefs(U);
|
|
|
|
|
|
|
|
// If the values are all Constants or Arguments, don't bother
|
2016-12-13 06:23:53 +08:00
|
|
|
if (llvm::none_of(Defs, isa<Instruction, Value *>))
|
2015-12-08 04:50:29 +08:00
|
|
|
return false;
|
|
|
|
|
2016-08-04 05:43:51 +08:00
|
|
|
// Presently, we only know how to handle PHINode, Constant, Arguments and
|
|
|
|
// CallInst. Potentially, bitwise operations (AND, OR, XOR, NOT) and sign
|
|
|
|
// extension could also be handled in the future.
|
2016-02-22 21:11:58 +08:00
|
|
|
for (Value *V : Defs)
|
2016-08-04 05:43:51 +08:00
|
|
|
if (!isa<PHINode>(V) && !isa<Constant>(V) &&
|
|
|
|
!isa<Argument>(V) && !isa<CallInst>(V))
|
2015-12-08 04:50:29 +08:00
|
|
|
return false;
|
|
|
|
|
2016-02-22 21:11:58 +08:00
|
|
|
for (Value *V : Defs)
|
2016-12-13 06:23:53 +08:00
|
|
|
if (const auto *P = dyn_cast<PHINode>(V))
|
2015-12-08 04:50:29 +08:00
|
|
|
if (!PromotablePHINodes.count(P))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (isa<ReturnInst>(U.getUser()))
|
|
|
|
++NumBoolRetPromotion;
|
|
|
|
if (isa<CallInst>(U.getUser()))
|
|
|
|
++NumBoolCallPromotion;
|
|
|
|
++NumBoolToIntPromotion;
|
|
|
|
|
2016-02-22 21:11:58 +08:00
|
|
|
for (Value *V : Defs)
|
2015-12-08 04:50:29 +08:00
|
|
|
if (!BoolToIntMap.count(V))
|
|
|
|
BoolToIntMap[V] = translate(V);
|
|
|
|
|
2016-08-04 05:43:51 +08:00
|
|
|
// Replace the operands of the translated instructions. They were set to
|
2015-12-08 04:50:29 +08:00
|
|
|
// zero in the translate function.
|
|
|
|
for (auto &Pair : BoolToIntMap) {
|
2016-12-13 06:23:53 +08:00
|
|
|
auto *First = dyn_cast<User>(Pair.first);
|
|
|
|
auto *Second = dyn_cast<User>(Pair.second);
|
2015-12-08 04:50:29 +08:00
|
|
|
assert((!First || Second) && "translated from user to non-user!?");
|
2016-08-04 05:43:51 +08:00
|
|
|
// Operands of CallInst are skipped because they may not be Bool type,
|
|
|
|
// and their positions are defined by ABI.
|
|
|
|
if (First && !isa<CallInst>(First))
|
2015-12-08 04:50:29 +08:00
|
|
|
for (unsigned i = 0; i < First->getNumOperands(); ++i)
|
|
|
|
Second->setOperand(i, BoolToIntMap[First->getOperand(i)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *IntRetVal = BoolToIntMap[U];
|
|
|
|
Type *Int1Ty = Type::getInt1Ty(U->getContext());
|
2016-12-13 06:23:53 +08:00
|
|
|
auto *I = cast<Instruction>(U.getUser());
|
2015-12-08 04:50:29 +08:00
|
|
|
Value *BackToBool = new TruncInst(IntRetVal, Int1Ty, "backToBool", I);
|
|
|
|
U.set(BackToBool);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-13 06:23:53 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2015-12-08 04:50:29 +08:00
|
|
|
AU.addPreserved<DominatorTreeWrapperPass>();
|
|
|
|
FunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
2017-06-09 02:27:24 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
const PPCSubtarget *ST;
|
2015-12-08 04:50:29 +08:00
|
|
|
};
|
2016-12-13 06:23:53 +08:00
|
|
|
|
|
|
|
} // end anonymous namespace
|
2015-12-08 04:50:29 +08:00
|
|
|
|
|
|
|
char PPCBoolRetToInt::ID = 0;
|
2017-03-31 10:16:54 +08:00
|
|
|
INITIALIZE_PASS(PPCBoolRetToInt, "bool-ret-to-int",
|
2017-06-09 02:27:24 +08:00
|
|
|
"Convert i1 constants to i32/i64 if they are returned",
|
2017-03-31 10:16:54 +08:00
|
|
|
false, false)
|
|
|
|
|
|
|
|
FunctionPass *llvm::createPPCBoolRetToIntPass() { return new PPCBoolRetToInt(); }
|