163f3ca5da75a614c603c04757edeaac123879d39Matt Arsenault//===-- ConstantFolding.h - Fold instructions into constants ----*- C++ -*-===//
2bd9d37026a5c17d9a51371a6a5446bf4761ee7d6John Criswell//
3bd9d37026a5c17d9a51371a6a5446bf4761ee7d6John Criswell//                     The LLVM Compiler Infrastructure
4bd9d37026a5c17d9a51371a6a5446bf4761ee7d6John Criswell//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
7bd9d37026a5c17d9a51371a6a5446bf4761ee7d6John Criswell//
8bd9d37026a5c17d9a51371a6a5446bf4761ee7d6John Criswell//===----------------------------------------------------------------------===//
9bd9d37026a5c17d9a51371a6a5446bf4761ee7d6John Criswell//
10b9b369fa9983843c4ed77b3a35b6e9b7933955bbDuncan Sands// This file declares routines for folding instructions into constants when all
11b9b369fa9983843c4ed77b3a35b6e9b7933955bbDuncan Sands// operands are constants, for example "sub i32 1, 0" -> "1".
1283e3c4f9d796a5da223a3ebd5d4ba985c7ecc39dDan Gohman//
1383e3c4f9d796a5da223a3ebd5d4ba985c7ecc39dDan Gohman// Also, to supplement the basic VMCore ConstantExpr simplifications,
1483e3c4f9d796a5da223a3ebd5d4ba985c7ecc39dDan Gohman// this file declares some additional folding routines that can make use of
153574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow// DataLayout information. These functions cannot go in VMCore due to library
1683e3c4f9d796a5da223a3ebd5d4ba985c7ecc39dDan Gohman// dependency issues.
17bd9d37026a5c17d9a51371a6a5446bf4761ee7d6John Criswell//
18bd9d37026a5c17d9a51371a6a5446bf4761ee7d6John Criswell//===----------------------------------------------------------------------===//
19bd9d37026a5c17d9a51371a6a5446bf4761ee7d6John Criswell
20c3ce6893742f0f2e20bffbbd7fb43b347fb0ece7Chris Lattner#ifndef LLVM_ANALYSIS_CONSTANTFOLDING_H
21c3ce6893742f0f2e20bffbbd7fb43b347fb0ece7Chris Lattner#define LLVM_ANALYSIS_CONSTANTFOLDING_H
22bd9d37026a5c17d9a51371a6a5446bf4761ee7d6John Criswell
23bd9d37026a5c17d9a51371a6a5446bf4761ee7d6John Criswellnamespace llvm {
24c3ce6893742f0f2e20bffbbd7fb43b347fb0ece7Chris Lattner  class Constant;
25c3ce6893742f0f2e20bffbbd7fb43b347fb0ece7Chris Lattner  class ConstantExpr;
26c3ce6893742f0f2e20bffbbd7fb43b347fb0ece7Chris Lattner  class Instruction;
273574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow  class DataLayout;
28618c1dbd293d15ee19f61b1156ab8086ad28311aChad Rosier  class TargetLibraryInfo;
29c3ce6893742f0f2e20bffbbd7fb43b347fb0ece7Chris Lattner  class Function;
30f286f6fd93d569befe6e77c94a947e6e04e95685Chris Lattner  class Type;
311d2f569c3428d70d0cf690c9adb459ad4a3ecff2Jay Foad  template<typename T>
321d2f569c3428d70d0cf690c9adb459ad4a3ecff2Jay Foad  class ArrayRef;
33bd9d37026a5c17d9a51371a6a5446bf4761ee7d6John Criswell
34b9b369fa9983843c4ed77b3a35b6e9b7933955bbDuncan Sands/// ConstantFoldInstruction - Try to constant fold the specified instruction.
35b9b369fa9983843c4ed77b3a35b6e9b7933955bbDuncan Sands/// If successful, the constant result is returned, if not, null is returned.
36b9b369fa9983843c4ed77b3a35b6e9b7933955bbDuncan Sands/// Note that this fails if not all of the operands are constant.  Otherwise,
37b9b369fa9983843c4ed77b3a35b6e9b7933955bbDuncan Sands/// this function can only fail when attempting to fold instructions like loads
38b9b369fa9983843c4ed77b3a35b6e9b7933955bbDuncan Sands/// and stores, which have no constant expression form.
394c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
404c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar                                    const TargetLibraryInfo *TLI = nullptr);
41c3ce6893742f0f2e20bffbbd7fb43b347fb0ece7Chris Lattner
423dfd7bf5110c47e99fd0fcce96122b90f699ca3aNick Lewycky/// ConstantFoldConstantExpression - Attempt to fold the constant expression
433574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow/// using the specified DataLayout.  If successful, the constant result is
443dfd7bf5110c47e99fd0fcce96122b90f699ca3aNick Lewycky/// result is returned, if not, null is returned.
454c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  Constant *
464c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  ConstantFoldConstantExpression(const ConstantExpr *CE, const DataLayout &DL,
474c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar                                 const TargetLibraryInfo *TLI = nullptr);
483dfd7bf5110c47e99fd0fcce96122b90f699ca3aNick Lewycky
49c3ce6893742f0f2e20bffbbd7fb43b347fb0ece7Chris Lattner/// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
50c3ce6893742f0f2e20bffbbd7fb43b347fb0ece7Chris Lattner/// specified operands.  If successful, the constant result is returned, if not,
5163f3ca5da75a614c603c04757edeaac123879d39Matt Arsenault/// null is returned.  Note that this function can fail when attempting to
5263f3ca5da75a614c603c04757edeaac123879d39Matt Arsenault/// fold instructions like loads and stores, which have no constant expression
53c3ce6893742f0f2e20bffbbd7fb43b347fb0ece7Chris Lattner/// form.
54c3ce6893742f0f2e20bffbbd7fb43b347fb0ece7Chris Lattner///
554c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  Constant *ConstantFoldInstOperands(unsigned Opcode, Type *DestTy,
564c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar                                     ArrayRef<Constant *> Ops,
574c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar                                     const DataLayout &DL,
584c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar                                     const TargetLibraryInfo *TLI = nullptr);
59f286f6fd93d569befe6e77c94a947e6e04e95685Chris Lattner
60f286f6fd93d569befe6e77c94a947e6e04e95685Chris Lattner/// ConstantFoldCompareInstOperands - Attempt to constant fold a compare
61f286f6fd93d569befe6e77c94a947e6e04e95685Chris Lattner/// instruction (icmp/fcmp) with the specified operands.  If it fails, it
62f286f6fd93d569befe6e77c94a947e6e04e95685Chris Lattner/// returns a constant expression of the specified operands.
63f286f6fd93d569befe6e77c94a947e6e04e95685Chris Lattner///
644c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  Constant *
654c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS,
664c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar                                  Constant *RHS, const DataLayout &DL,
674c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar                                  const TargetLibraryInfo *TLI = nullptr);
68c3ce6893742f0f2e20bffbbd7fb43b347fb0ece7Chris Lattner
69dabc2806726a286b00313419fac8461ebe0f774cDuncan Sands/// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue
70dabc2806726a286b00313419fac8461ebe0f774cDuncan Sands/// instruction with the specified operands and indices.  The constant result is
71dabc2806726a286b00313419fac8461ebe0f774cDuncan Sands/// returned if successful; if not, null is returned.
72dabc2806726a286b00313419fac8461ebe0f774cDuncan SandsConstant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val,
73dabc2806726a286b00313419fac8461ebe0f774cDuncan Sands                                             ArrayRef<unsigned> Idxs);
74dabc2806726a286b00313419fac8461ebe0f774cDuncan Sands
75878e4946700824954d7eb7f3ff660353db8e0d17Chris Lattner/// ConstantFoldLoadFromConstPtr - Return the value that a load from C would
76878e4946700824954d7eb7f3ff660353db8e0d17Chris Lattner/// produce if it is constant and determinable.  If this is not determinable,
77878e4946700824954d7eb7f3ff660353db8e0d17Chris Lattner/// return null.
784c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga NainarConstant *ConstantFoldLoadFromConstPtr(Constant *C, const DataLayout &DL);
79c3ce6893742f0f2e20bffbbd7fb43b347fb0ece7Chris Lattner
80c3ce6893742f0f2e20bffbbd7fb43b347fb0ece7Chris Lattner/// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a
81c3ce6893742f0f2e20bffbbd7fb43b347fb0ece7Chris Lattner/// getelementptr constantexpr, return the constant value being addressed by the
82c3ce6893742f0f2e20bffbbd7fb43b347fb0ece7Chris Lattner/// constant expression, or null if something is funny and we can't decide.
83c6f69e94fa46f585b59bb9d7ace3224b0e638c95Dan GohmanConstant *ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE);
84a97ddd0cffb9a9eb1313343fd49097c9057a5850Chris Lattner
85a97ddd0cffb9a9eb1313343fd49097c9057a5850Chris Lattner/// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr
86a97ddd0cffb9a9eb1313343fd49097c9057a5850Chris Lattner/// indices (with an *implied* zero pointer index that is not in the list),
87a97ddd0cffb9a9eb1313343fd49097c9057a5850Chris Lattner/// return the constant value being addressed by a virtual load, or null if
88a97ddd0cffb9a9eb1313343fd49097c9057a5850Chris Lattner/// something is funny and we can't decide.
89a97ddd0cffb9a9eb1313343fd49097c9057a5850Chris LattnerConstant *ConstantFoldLoadThroughGEPIndices(Constant *C,
90a97ddd0cffb9a9eb1313343fd49097c9057a5850Chris Lattner                                            ArrayRef<Constant*> Indices);
91a97ddd0cffb9a9eb1313343fd49097c9057a5850Chris Lattner
92bd9d37026a5c17d9a51371a6a5446bf4761ee7d6John Criswell/// canConstantFoldCallTo - Return true if its even possible to fold a call to
93bd9d37026a5c17d9a51371a6a5446bf4761ee7d6John Criswell/// the specified function.
94fa9b80eb64127b3d9691e18537975635520e51e9Dan Gohmanbool canConstantFoldCallTo(const Function *F);
95bd9d37026a5c17d9a51371a6a5446bf4761ee7d6John Criswell
96bd9d37026a5c17d9a51371a6a5446bf4761ee7d6John Criswell/// ConstantFoldCall - Attempt to constant fold a call to the specified function
97bd9d37026a5c17d9a51371a6a5446bf4761ee7d6John Criswell/// with the specified arguments, returning null if unsuccessful.
98618c1dbd293d15ee19f61b1156ab8086ad28311aChad RosierConstant *ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
99dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                           const TargetLibraryInfo *TLI = nullptr);
100bd9d37026a5c17d9a51371a6a5446bf4761ee7d6John Criswell}
101bd9d37026a5c17d9a51371a6a5446bf4761ee7d6John Criswell
102c3ce6893742f0f2e20bffbbd7fb43b347fb0ece7Chris Lattner#endif
103