blob: 93c201d03869096e7605b4f98610d8c4c3abe3b4 [file] [log] [blame]
Eugene Zelenko6a9226d2016-12-12 22:23:53 +00001//===- PPCBoolRetToInt.cpp ------------------------------------------------===//
Kit Bartona1c712f2015-12-07 20:50:29 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Eric Christopher9fd267c2017-03-31 02:16:54 +000010// This file implements converting i1 values to i32 if they could be more
Kit Bartona1c712f2015-12-07 20:50:29 +000011// profitably allocated as GPRs rather than CRs. This pass will become totally
12// unnecessary if Register Bank Allocation and Global Instruction Selection ever
13// go upstream.
14//
Eric Christopher9fd267c2017-03-31 02:16:54 +000015// Presently, the pass converts i1 Constants, and Arguments to i32 if the
Kit Bartona1c712f2015-12-07 20:50:29 +000016// transitive closure of their uses includes only PHINodes, CallInsts, and
17// ReturnInsts. The rational is that arguments are generally passed and returned
Eric Christopher9fd267c2017-03-31 02:16:54 +000018// in GPRs rather than CRs, so casting them to i32 at the LLVM IR level will
Kit Bartona1c712f2015-12-07 20:50:29 +000019// actually save casts at the Machine Instruction level.
20//
21// It might be useful to expand this pass to add bit-wise operations to the list
22// of safe transitive closure types. Also, we miss some opportunities when LLVM
23// represents logical AND and OR operations with control flow rather than data
24// flow. For example by lowering the expression: return (A && B && C)
25//
26// as: return A ? true : B && C.
27//
28// There's code in SimplifyCFG that code be used to turn control flow in data
29// flow using SelectInsts. Selects are slow on some architectures (P7/P8), so
30// this probably isn't good in general, but for the special case of i1, the
31// Selects could be further lowered to bit operations that are fast everywhere.
32//
33//===----------------------------------------------------------------------===//
34
35#include "PPC.h"
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000036#include "llvm/ADT/DenseMap.h"
Kit Bartona1c712f2015-12-07 20:50:29 +000037#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000038#include "llvm/ADT/SmallVector.h"
Kit Bartona1c712f2015-12-07 20:50:29 +000039#include "llvm/ADT/Statistic.h"
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000040#include "llvm/ADT/STLExtras.h"
41#include "llvm/IR/Argument.h"
Kit Bartona1c712f2015-12-07 20:50:29 +000042#include "llvm/IR/Constants.h"
43#include "llvm/IR/Dominators.h"
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000044#include "llvm/IR/Function.h"
45#include "llvm/IR/Instruction.h"
Kit Bartona1c712f2015-12-07 20:50:29 +000046#include "llvm/IR/Instructions.h"
47#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000048#include "llvm/IR/OperandTraits.h"
49#include "llvm/IR/Type.h"
50#include "llvm/IR/Use.h"
51#include "llvm/IR/User.h"
52#include "llvm/IR/Value.h"
53#include "llvm/Support/Casting.h"
Kit Bartona1c712f2015-12-07 20:50:29 +000054#include "llvm/Pass.h"
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000055#include <cassert>
Kit Bartona1c712f2015-12-07 20:50:29 +000056
57using namespace llvm;
58
59namespace {
60
61#define DEBUG_TYPE "bool-ret-to-int"
62
63STATISTIC(NumBoolRetPromotion,
64 "Number of times a bool feeding a RetInst was promoted to an int");
65STATISTIC(NumBoolCallPromotion,
66 "Number of times a bool feeding a CallInst was promoted to an int");
67STATISTIC(NumBoolToIntPromotion,
68 "Total number of times a bool was promoted to an int");
69
70class PPCBoolRetToInt : public FunctionPass {
Kit Bartona1c712f2015-12-07 20:50:29 +000071 static SmallPtrSet<Value *, 8> findAllDefs(Value *V) {
72 SmallPtrSet<Value *, 8> Defs;
73 SmallVector<Value *, 8> WorkList;
74 WorkList.push_back(V);
75 Defs.insert(V);
76 while (!WorkList.empty()) {
77 Value *Curr = WorkList.back();
78 WorkList.pop_back();
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000079 auto *CurrUser = dyn_cast<User>(Curr);
Guozhi Wei9584d182016-08-03 21:43:51 +000080 // Operands of CallInst are skipped because they may not be Bool type,
81 // and their positions are defined by ABI.
82 if (CurrUser && !isa<CallInst>(Curr))
Kit Bartona1c712f2015-12-07 20:50:29 +000083 for (auto &Op : CurrUser->operands())
84 if (Defs.insert(Op).second)
85 WorkList.push_back(Op);
86 }
87 return Defs;
88 }
89
Eric Christopher9fd267c2017-03-31 02:16:54 +000090 // Translate a i1 value to an equivalent i32 value:
91 static Value *translate(Value *V) {
92 Type *Int32Ty = Type::getInt32Ty(V->getContext());
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000093 if (auto *C = dyn_cast<Constant>(V))
Eric Christopher9fd267c2017-03-31 02:16:54 +000094 return ConstantExpr::getZExt(C, Int32Ty);
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000095 if (auto *P = dyn_cast<PHINode>(V)) {
Kit Bartona1c712f2015-12-07 20:50:29 +000096 // Temporarily set the operands to 0. We'll fix this later in
97 // runOnUse.
Eric Christopher9fd267c2017-03-31 02:16:54 +000098 Value *Zero = Constant::getNullValue(Int32Ty);
Kit Bartona1c712f2015-12-07 20:50:29 +000099 PHINode *Q =
Eric Christopher9fd267c2017-03-31 02:16:54 +0000100 PHINode::Create(Int32Ty, P->getNumIncomingValues(), P->getName(), P);
Kit Bartona1c712f2015-12-07 20:50:29 +0000101 for (unsigned i = 0; i < P->getNumOperands(); ++i)
102 Q->addIncoming(Zero, P->getIncomingBlock(i));
103 return Q;
104 }
105
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000106 auto *A = dyn_cast<Argument>(V);
107 auto *I = dyn_cast<Instruction>(V);
Kit Bartona1c712f2015-12-07 20:50:29 +0000108 assert((A || I) && "Unknown value type");
109
110 auto InstPt =
111 A ? &*A->getParent()->getEntryBlock().begin() : I->getNextNode();
Eric Christopher9fd267c2017-03-31 02:16:54 +0000112 return new ZExtInst(V, Int32Ty, "", InstPt);
Kit Bartona1c712f2015-12-07 20:50:29 +0000113 }
114
115 typedef SmallPtrSet<const PHINode *, 8> PHINodeSet;
116
117 // A PHINode is Promotable if:
118 // 1. Its type is i1 AND
119 // 2. All of its uses are ReturnInt, CallInst, PHINode, or DbgInfoIntrinsic
120 // AND
121 // 3. All of its operands are Constant or Argument or
122 // CallInst or PHINode AND
123 // 4. All of its PHINode uses are Promotable AND
124 // 5. All of its PHINode operands are Promotable
125 static PHINodeSet getPromotablePHINodes(const Function &F) {
126 PHINodeSet Promotable;
127 // Condition 1
128 for (auto &BB : F)
129 for (auto &I : BB)
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000130 if (const auto *P = dyn_cast<PHINode>(&I))
Kit Bartona1c712f2015-12-07 20:50:29 +0000131 if (P->getType()->isIntegerTy(1))
132 Promotable.insert(P);
133
134 SmallVector<const PHINode *, 8> ToRemove;
Benjamin Kramer451f54c2016-02-22 13:11:58 +0000135 for (const PHINode *P : Promotable) {
Kit Bartona1c712f2015-12-07 20:50:29 +0000136 // Condition 2 and 3
137 auto IsValidUser = [] (const Value *V) -> bool {
138 return isa<ReturnInst>(V) || isa<CallInst>(V) || isa<PHINode>(V) ||
139 isa<DbgInfoIntrinsic>(V);
140 };
141 auto IsValidOperand = [] (const Value *V) -> bool {
142 return isa<Constant>(V) || isa<Argument>(V) || isa<CallInst>(V) ||
143 isa<PHINode>(V);
144 };
145 const auto &Users = P->users();
146 const auto &Operands = P->operands();
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000147 if (!llvm::all_of(Users, IsValidUser) ||
148 !llvm::all_of(Operands, IsValidOperand))
Kit Bartona1c712f2015-12-07 20:50:29 +0000149 ToRemove.push_back(P);
150 }
151
152 // Iterate to convergence
153 auto IsPromotable = [&Promotable] (const Value *V) -> bool {
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000154 const auto *Phi = dyn_cast<PHINode>(V);
Kit Bartona1c712f2015-12-07 20:50:29 +0000155 return !Phi || Promotable.count(Phi);
156 };
157 while (!ToRemove.empty()) {
158 for (auto &User : ToRemove)
159 Promotable.erase(User);
160 ToRemove.clear();
161
Benjamin Kramer451f54c2016-02-22 13:11:58 +0000162 for (const PHINode *P : Promotable) {
Kit Bartona1c712f2015-12-07 20:50:29 +0000163 // Condition 4 and 5
164 const auto &Users = P->users();
165 const auto &Operands = P->operands();
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000166 if (!llvm::all_of(Users, IsPromotable) ||
167 !llvm::all_of(Operands, IsPromotable))
Kit Bartona1c712f2015-12-07 20:50:29 +0000168 ToRemove.push_back(P);
169 }
170 }
171
172 return Promotable;
173 }
174
175 typedef DenseMap<Value *, Value *> B2IMap;
176
177 public:
178 static char ID;
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000179
Eric Christopher9fd267c2017-03-31 02:16:54 +0000180 PPCBoolRetToInt() : FunctionPass(ID) {
Kit Bartona1c712f2015-12-07 20:50:29 +0000181 initializePPCBoolRetToIntPass(*PassRegistry::getPassRegistry());
182 }
183
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000184 bool runOnFunction(Function &F) override {
Andrew Kaylor289bd5f2016-04-27 19:39:32 +0000185 if (skipFunction(F))
186 return false;
187
Kit Bartona1c712f2015-12-07 20:50:29 +0000188 PHINodeSet PromotablePHINodes = getPromotablePHINodes(F);
189 B2IMap Bool2IntMap;
190 bool Changed = false;
191 for (auto &BB : F) {
192 for (auto &I : BB) {
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000193 if (auto *R = dyn_cast<ReturnInst>(&I))
Kit Bartona1c712f2015-12-07 20:50:29 +0000194 if (F.getReturnType()->isIntegerTy(1))
195 Changed |=
196 runOnUse(R->getOperandUse(0), PromotablePHINodes, Bool2IntMap);
197
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000198 if (auto *CI = dyn_cast<CallInst>(&I))
Kit Bartona1c712f2015-12-07 20:50:29 +0000199 for (auto &U : CI->operands())
200 if (U->getType()->isIntegerTy(1))
201 Changed |= runOnUse(U, PromotablePHINodes, Bool2IntMap);
202 }
203 }
204
205 return Changed;
206 }
207
Eric Christopher9fd267c2017-03-31 02:16:54 +0000208 static bool runOnUse(Use &U, const PHINodeSet &PromotablePHINodes,
Kit Bartona1c712f2015-12-07 20:50:29 +0000209 B2IMap &BoolToIntMap) {
210 auto Defs = findAllDefs(U);
211
212 // If the values are all Constants or Arguments, don't bother
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000213 if (llvm::none_of(Defs, isa<Instruction, Value *>))
Kit Bartona1c712f2015-12-07 20:50:29 +0000214 return false;
215
Guozhi Wei9584d182016-08-03 21:43:51 +0000216 // Presently, we only know how to handle PHINode, Constant, Arguments and
217 // CallInst. Potentially, bitwise operations (AND, OR, XOR, NOT) and sign
218 // extension could also be handled in the future.
Benjamin Kramer451f54c2016-02-22 13:11:58 +0000219 for (Value *V : Defs)
Guozhi Wei9584d182016-08-03 21:43:51 +0000220 if (!isa<PHINode>(V) && !isa<Constant>(V) &&
221 !isa<Argument>(V) && !isa<CallInst>(V))
Kit Bartona1c712f2015-12-07 20:50:29 +0000222 return false;
223
Benjamin Kramer451f54c2016-02-22 13:11:58 +0000224 for (Value *V : Defs)
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000225 if (const auto *P = dyn_cast<PHINode>(V))
Kit Bartona1c712f2015-12-07 20:50:29 +0000226 if (!PromotablePHINodes.count(P))
227 return false;
228
229 if (isa<ReturnInst>(U.getUser()))
230 ++NumBoolRetPromotion;
231 if (isa<CallInst>(U.getUser()))
232 ++NumBoolCallPromotion;
233 ++NumBoolToIntPromotion;
234
Benjamin Kramer451f54c2016-02-22 13:11:58 +0000235 for (Value *V : Defs)
Kit Bartona1c712f2015-12-07 20:50:29 +0000236 if (!BoolToIntMap.count(V))
237 BoolToIntMap[V] = translate(V);
238
Guozhi Wei9584d182016-08-03 21:43:51 +0000239 // Replace the operands of the translated instructions. They were set to
Kit Bartona1c712f2015-12-07 20:50:29 +0000240 // zero in the translate function.
241 for (auto &Pair : BoolToIntMap) {
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000242 auto *First = dyn_cast<User>(Pair.first);
243 auto *Second = dyn_cast<User>(Pair.second);
Kit Bartona1c712f2015-12-07 20:50:29 +0000244 assert((!First || Second) && "translated from user to non-user!?");
Guozhi Wei9584d182016-08-03 21:43:51 +0000245 // Operands of CallInst are skipped because they may not be Bool type,
246 // and their positions are defined by ABI.
247 if (First && !isa<CallInst>(First))
Kit Bartona1c712f2015-12-07 20:50:29 +0000248 for (unsigned i = 0; i < First->getNumOperands(); ++i)
249 Second->setOperand(i, BoolToIntMap[First->getOperand(i)]);
250 }
251
252 Value *IntRetVal = BoolToIntMap[U];
253 Type *Int1Ty = Type::getInt1Ty(U->getContext());
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000254 auto *I = cast<Instruction>(U.getUser());
Kit Bartona1c712f2015-12-07 20:50:29 +0000255 Value *BackToBool = new TruncInst(IntRetVal, Int1Ty, "backToBool", I);
256 U.set(BackToBool);
257
258 return true;
259 }
260
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000261 void getAnalysisUsage(AnalysisUsage &AU) const override {
Kit Bartona1c712f2015-12-07 20:50:29 +0000262 AU.addPreserved<DominatorTreeWrapperPass>();
263 FunctionPass::getAnalysisUsage(AU);
264 }
265};
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000266
267} // end anonymous namespace
Kit Bartona1c712f2015-12-07 20:50:29 +0000268
269char PPCBoolRetToInt::ID = 0;
Eric Christopher9fd267c2017-03-31 02:16:54 +0000270INITIALIZE_PASS(PPCBoolRetToInt, "bool-ret-to-int",
271 "Convert i1 constants to i32 if they are returned",
272 false, false)
Kit Bartona1c712f2015-12-07 20:50:29 +0000273
Eric Christopher9fd267c2017-03-31 02:16:54 +0000274FunctionPass *llvm::createPPCBoolRetToIntPass() { return new PPCBoolRetToInt(); }